--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    天赋：突破音速 sound_barrier_break

    【技能】【宇宙之翼】
        背上出现翅膀。（移植闪光路径特效，懒人手杖羊头皮肤那个）
        穿戴后可在水面上行走。移动速度X1.5。饥饿值降低速度X1.5。
        饥饿为0的时候强制解除。
        【天赋】【突破音速】关闭状态下只提供基础加速效果，不再加速掉电，开启状态下还可以踏水。

]]--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 水上行走
    local OCEAN_WALK = TUNING["loramia.Config"].WING_OF_THE_UNIVERSE_OCEAN_WALK or false
    local SPEED_MULT = (1 + TUNING["loramia.Config"].WING_OF_THE_UNIVERSE_SPEED_MULT ) or 2

    local WINGS_OF_UNIVERSE_TALENT = "sound_barrier_break"
    
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 水上行走
    local function turn_on_player_ocean_walk(owner)
        if not OCEAN_WALK then
            return
        end
        if owner.components.drownable and owner.components.drownable.enabled ~= false then
            owner.components.drownable.enabled = false
        end
        owner.Physics:ClearCollisionMask()
        owner.Physics:CollidesWith(COLLISION.GROUND)
        owner.Physics:CollidesWith(COLLISION.OBSTACLES)
        owner.Physics:CollidesWith(COLLISION.SMALLOBSTACLES)
        owner.Physics:CollidesWith(COLLISION.CHARACTERS)
        owner.Physics:CollidesWith(COLLISION.GIANTS)
        owner.Physics:Teleport(owner.Transform:GetWorldPosition())
    end
    local function turn_off_player_ocean_walk(owner)
        if not OCEAN_WALK then
            return
        end
        if owner.components.drownable then
            owner.components.drownable.enabled = true
        end
        owner.Physics:ClearCollisionMask()
        owner.Physics:CollidesWith(COLLISION.WORLD)
        owner.Physics:CollidesWith(COLLISION.OBSTACLES)
        owner.Physics:CollidesWith(COLLISION.SMALLOBSTACLES)
        owner.Physics:CollidesWith(COLLISION.CHARACTERS)
        owner.Physics:CollidesWith(COLLISION.GIANTS)
        owner.Physics:Teleport(owner.Transform:GetWorldPosition())
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

return function(inst)
    if not TheWorld.ismastersim then
        return
    end
    ----------------------------------------------------------------------------------
    --- 基础API
        local wings = nil
        local function RemoveWings()
            -- if wings and wings:IsValid() then
            --     wings:Remove()
            -- end
            wings = nil
        end
        local function SetWings(item)
            wings = item
        end
        local function GetWings()
            if wings and wings:IsValid() then
                return wings
            end
            return nil
        end
        local mult_inst = CreateEntity()
        inst:ListenForEvent("onremove",function()
            mult_inst:Remove()
        end)
    ----------------------------------------------------------------------------------
    --- 星光特效
        local sfx_inst = nil
        local function create_fx()
            if sfx_inst and sfx_inst:IsValid() then
                sfx_inst:Remove()
            end
            sfx_inst = SpawnPrefab("loramia_sfx_wings_of_universe")
            sfx_inst.entity:SetParent(inst.entity)
            sfx_inst.entity:AddFollower()
            sfx_inst.Follower:FollowSymbol(inst.GUID, "swap_body", 0, 0, 0,true)
            sfx_inst.sfx = {}
            for i = 1, 6, 1 do
                local fx = SpawnPrefab("cane_victorian_fx")
                fx.entity:SetParent(sfx_inst.entity)
                fx.entity:AddFollower()
                fx.Follower:FollowSymbol(sfx_inst.GUID, "point_"..i,0,0,0,true)
                table.insert(sfx_inst.sfx,fx)
            end
        end
        local function remove_fx()
            if sfx_inst and sfx_inst:IsValid() then
                sfx_inst:Remove()
            end
            sfx_inst = nil
        end
    ----------------------------------------------------------------------------------
    --- 移动速度加成 天赋解锁后永久生效
        local function Active_Speed_Mult()
            inst.components.locomotor:SetExternalSpeedMultiplier(mult_inst, "wings_of_universe", 1.5)
        end
        local function Deactive_Speed_Mult()
            if inst.components.loramia_com_talent_tree:IsUnlocked(WINGS_OF_UNIVERSE_TALENT) then
                return
            end
            inst.components.locomotor:SetExternalSpeedMultiplier(mult_inst, "wings_of_universe", 1)
        end
        inst:DoTaskInTime(0,function()
            if inst.components.loramia_com_talent_tree:IsUnlocked(WINGS_OF_UNIVERSE_TALENT) then
                Active_Speed_Mult()
            end
        end)
    ----------------------------------------------------------------------------------
    --- 饥饿值消耗加成 天赋解锁后 永久失效
        local function Active_Hunger_Mult()
            if inst.components.loramia_com_talent_tree:IsUnlocked(WINGS_OF_UNIVERSE_TALENT) then
                inst.components.hunger.burnratemodifiers:SetModifier(mult_inst, 1)
                return
            end
            inst.components.hunger.burnratemodifiers:SetModifier(mult_inst, 1.5)
        end
        local function Deactive_Hunger_Mult()
            inst.components.hunger.burnratemodifiers:SetModifier(mult_inst, 1)
        end
    ----------------------------------------------------------------------------------
    --- 装备特效
        inst:ListenForEvent("loramia_event.wings_of_universe_onequip",function(_,item)
            inst.AnimState:OverrideSymbol("swap_body", "loramia_item_wings_of_universe", "swap_body")
            SetWings(item)
            if OCEAN_WALK then
                turn_on_player_ocean_walk(inst)
            end
            Active_Speed_Mult()
            Active_Hunger_Mult()
            create_fx()
        end)
        inst:ListenForEvent("loramia_event.wings_of_universe_unequip",function(_,item)
            inst.AnimState:ClearOverrideSymbol("swap_body")
            RemoveWings()
            if OCEAN_WALK then
                turn_off_player_ocean_walk(inst)
            end
            Deactive_Speed_Mult()
            Deactive_Hunger_Mult()
            remove_fx()
        end)
    ----------------------------------------------------------------------------------
    --- 解锁技能树、重置技能树的时候，移除翅膀
        inst:ListenForEvent("loramia_com_talent_tree_unlock",function(inst,talent_name)
            if talent_name == WINGS_OF_UNIVERSE_TALENT then
                RemoveWings()
                Active_Speed_Mult()
            end
        end)
        inst:ListenForEvent("loramia_com_talent_tree_reset",RemoveWings)
    ----------------------------------------------------------------------------------
    --- 技能激活这个event
        inst:ListenForEvent("loramia_event.wings_of_universe_switch",function()
            local item = GetWings()
            if item then
                -- inst.components.inventory:Unequip(item.components.equippable.equipslot)
                -- inst:PushEvent("loramia_event.wings_of_universe_unequip",item)
                -- item:Remove()
                inst.components.inventory:DropItem(item)
                RemoveWings()
            elseif not inst.components.hunger:IsStarving() then
                inst.components.inventory:Equip(SpawnPrefab("loramia_item_wings_of_universe"))
            end
        end)
    ----------------------------------------------------------------------------------
    --- 饥饿为0的时候强制解除。
        inst:ListenForEvent("startstarving",function()
            local item = GetWings()
            if item then
                -- inst:PushEvent("loramia_event.wings_of_universe_unequip",item)
                -- inst.components.inventory:Unequip(item.components.equippable.equipslot)
                -- item:Remove()
                inst.components.inventory:DropItem(item)
                RemoveWings()
            end
        end)
    ----------------------------------------------------------------------------------



end